package com.ruoyi.scheduledTasks;

import com.ruoyi.common.core.domain.entity.AiRecordMatchRate;
import com.ruoyi.common.core.domain.entity.AiRecordingChild;
import com.ruoyi.common.core.domain.entity.AiRecordingMain;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.mapper.AiRecordMatchRateMapper;
import com.ruoyi.system.mapper.AiRecordingChildMapper;
import com.ruoyi.system.mapper.AiRecordingMainMapper;
import com.ruoyi.system.util.deepseek.LevenshteinDistance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Component("recordMatchRateTask")
public class AiRecordMatchRateTask {
//    @Autowired
//    private AiRecordingMainMapper aiRecordingMainMapper;
//    @Autowired
//    private AiRecordingChildMapper aiRecordingChildMapper;

//    private LevenshteinDistance levenshteinDistance;
////    @Autowired
////    private AiRecordMatchRateMapper aiRecordMatchRateMapper;
    public void getRate() {
        AiRecordingMain aiRecordingMain = new AiRecordingMain();
        aiRecordingMain.setMatching("0");
        AiRecordingChild aiRecordingChild=new AiRecordingChild();
        aiRecordingChild.setDelFlag("0");
        AiRecordingChildMapper aiRecordingChildMapper= SpringUtils.getBean(AiRecordingChildMapper.class);
        AiRecordingMainMapper aiRecordingMainMapper= SpringUtils.getBean(AiRecordingMainMapper.class);
        AiRecordMatchRateMapper aiRecordMatchRateMapper= SpringUtils.getBean(AiRecordMatchRateMapper.class);

        List<AiRecordingChild>  aiRecordingChildList= aiRecordingChildMapper.selectGroupMainId();
       List<AiRecordingMain>  aiRecordingMainList= aiRecordingMainMapper.selectAiRecordingMainList(aiRecordingMain);

        aiRecordingMainList.forEach(aiRecordingMain1 -> {
            Long mainId=aiRecordingMain1.getId();
            System.out.println("录音--"+mainId+"正在匹配");

            List<AiRecordingChild> filteredList = aiRecordingChildList.stream()
                    .filter(person -> person.getMainId().equals(mainId)) // 过滤条件
                    .collect(Collectors.toList()); // 收集结果
            if (!filteredList.isEmpty()){
                aiRecordingChildList.forEach(child->{
                  Long   matchMainId =   child.getMainId();
                    if (!matchMainId.equals(mainId)){
                        double task=   similarity(filteredList.get(0).getTranscript(), child.getTranscript());
                         if (task>=0.7){
                             if (task==1.0){
                                 System.out.println("完全匹配");
                             }
                             AiRecordMatchRate aiRecordMatchRate=new AiRecordMatchRate();
                             aiRecordMatchRate.setMainId(mainId);
                             aiRecordMatchRate.setMatchMainId(matchMainId);
                             aiRecordMatchRate.setMatchRate(task+"");
                             aiRecordMatchRate.setCreateTime(new Date());

                             aiRecordMatchRate.setMatchTime(child.getCreateTime());
                             aiRecordMatchRate.setMatchBy(child.getCreateBy());
                             aiRecordMatchRate.setMatchUserId(child.getUserId());


                             aiRecordMatchRate.setMainTime(aiRecordingMain1.getCreateTime());
                             aiRecordMatchRate.setMainBy(aiRecordingMain1.getCreateBy());
                             aiRecordMatchRate.setMainUserId(aiRecordingMain1.getUserId());
                             aiRecordMatchRateMapper.insertAiRecordMatchRate(aiRecordMatchRate);
                         }
                    }
                });
            }
            aiRecordingMain1.setMatching("1");
            aiRecordingMainMapper.updateAiRecordingMain(aiRecordingMain1);
        });
    }


    public static int calculate(String str1, String str2) {
        int[][] dp = new int[str1.length() + 1][str2.length() + 1];

        for (int i = 0; i <= str1.length(); i++) {
            for (int j = 0; j <= str2.length(); j++) {
                if (i == 0) {
                    dp[i][j] = j;
                } else if (j == 0) {
                    dp[i][j] = i;
                } else {
                    dp[i][j] = min(dp[i - 1][j - 1] + costOfSubstitution(str1.charAt(i - 1), str2.charAt(j - 1)),
                            dp[i - 1][j] + 1,
                            dp[i][j - 1] + 1);
                }
            }
        }

        return dp[str1.length()][str2.length()];
    }

    private static int costOfSubstitution(char a, char b) {
        return a == b ? 0 : 1;
    }

    private static int min(int... numbers) {
        return Arrays.stream(numbers).min().orElse(Integer.MAX_VALUE);
    }

    public  double similarity(String str1, String str2) {
        int maxLength = Math.max(str1.length(), str2.length());
        if (maxLength == 0) return 1.0;
        return 1.0 - (double) calculate(str1, str2) / maxLength;
    }


}
